package com.zkx.blogweb.entity;

/**
 * @author zkx
 */
public class Message<T> {

    private int code; // 状态码
    private String msg; // 返回的信息
    private T data; // 返回的数据
    private String toUrl;

    private Message(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    private Message(CodeMsg cm) {
        if (cm == null){
            return ;
        }
        this.code = cm.getCode();
        this.msg = cm.getMsg();
    }

    /**
     * 成功时候的调用
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Message<T> success(T data){
        return new Message<T>(data);
    }

    public Message(T data, String url) {
        this.data = data;
        this.toUrl = url;
    }

    public static <T> Message<T> success(T data, String url){
        return new Message<T>(data,url);
    }

    /**
     * 异常时候的调用
     * @param cm
     * @param <T>
     * @return
     */
    public static <T> Message<T> error(CodeMsg cm){
        return new Message<T>(cm);
    }

    public static <T> Message<T> error(T msg, String url){
        return new Message<T>(msg,url);
    }

    public String getToUrl() {
        return toUrl;
    }

    public void setToUrl(String toUrl) {
        this.toUrl = toUrl;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

}
